home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 2002 November / SGI Freeware 2002 November - Disc 3.iso / dist / fw_qt3.idb / usr / freeware / Qt / examples / tooltip / tooltip.cpp.z / tooltip.cpp
C/C++ Source or Header  |  2002-04-08  |  2KB  |  118 lines

  1. /****************************************************************************
  2. ** $Id:  qt/tooltip.cpp   3.0.3   edited Oct 12 12:18 $
  3. **
  4. ** Copyright (C) 1992-2000 Trolltech AS.  All rights reserved.
  5. **
  6. ** This file is part of an example program for Qt.  This example
  7. ** program may be used, distributed and modified without limitation.
  8. **
  9. *****************************************************************************/
  10.  
  11. #include "tooltip.h"
  12. #include <qapplication.h>
  13. #include <qpainter.h>
  14. #include <stdlib.h>
  15.  
  16.  
  17. DynamicTip::DynamicTip( QWidget * parent )
  18.     : QToolTip( parent )
  19. {
  20.     // no explicit initialization needed
  21. }
  22.  
  23.  
  24. void DynamicTip::maybeTip( const QPoint &pos )
  25. {
  26.     if ( !parentWidget()->inherits( "TellMe" ) )
  27.     return;
  28.  
  29.     QRect r( ((TellMe*)parentWidget())->tip(pos) );
  30.     if ( !r.isValid() )
  31.     return;
  32.  
  33.     QString s;
  34.     s.sprintf( "position: %d,%d", r.center().x(), r.center().y() );
  35.     tip( r, s );
  36. }
  37.  
  38.  
  39. TellMe::TellMe( QWidget * parent , const char * name  )
  40.     : QWidget( parent, name )
  41. {
  42.     setMinimumSize( 30, 30 );
  43.     r1 = randomRect();
  44.     r2 = randomRect();
  45.     r3 = randomRect();
  46.  
  47.     t = new DynamicTip( this );
  48.  
  49.     QToolTip::add( this, r3, "this color is called red" ); // <- helpful
  50. }
  51.  
  52.  
  53. TellMe::~TellMe()
  54. {
  55.     delete t;
  56.     t = 0;
  57. }
  58.  
  59.  
  60. void TellMe::paintEvent( QPaintEvent * e )
  61. {
  62.     QPainter p( this );
  63.  
  64.     // I try to be efficient here, and repaint only what's needed
  65.  
  66.     if ( e->rect().intersects( r1 ) ) {
  67.     p.setBrush( blue );
  68.     p.drawRect( r1 );
  69.     }
  70.  
  71.     if ( e->rect().intersects( r2 ) ) {
  72.     p.setBrush( blue );
  73.     p.drawRect( r2 );
  74.     }
  75.  
  76.     if ( e->rect().intersects( r3 ) ) {
  77.     p.setBrush( red );
  78.     p.drawRect( r3 );
  79.     }
  80. }
  81.  
  82.  
  83. void TellMe::mousePressEvent( QMouseEvent * e )
  84. {
  85.     if ( r1.contains( e->pos() ) )
  86.     r1 = randomRect();
  87.     if ( r2.contains( e->pos() ) )
  88.     r2 = randomRect();
  89.     repaint();
  90. }
  91.  
  92.  
  93. void TellMe::resizeEvent( QResizeEvent * )
  94. {
  95.     if ( !rect().contains( r1 ) )
  96.      r1 = randomRect();
  97.     if ( !rect().contains( r2 ) )
  98.      r2 = randomRect();
  99. }
  100.  
  101.  
  102. QRect TellMe::randomRect()
  103. {
  104.     return QRect( ::rand() % (width() - 20), ::rand() % (height() - 20),
  105.           20, 20 );
  106. }
  107.  
  108.  
  109. QRect TellMe::tip( const QPoint & p )
  110. {
  111.     if ( r1.contains( p ) )
  112.     return r1;
  113.     else if ( r2.contains( p ) )
  114.     return r2;
  115.     else
  116.     return QRect( 0,0, -1,-1 );
  117. }
  118.